package cn.bjit.controller;


import cn.bjit.enums.CouponCategoryEnum;
import cn.bjit.request.NewUserCouponRequest;
import cn.bjit.service.CouponService;
import cn.bjit.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yx
 * @since 2022-06-14
 */
@Slf4j
@Api(tags = "优惠券模块")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {
    @Resource
    private CouponService couponService;
    @Resource
    private RedissonClient redissonClient;

    @ApiOperation("分页查询优惠券")
    @GetMapping("page_coupon")
    public JsonData pageCouponList(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "10") int size) {
        Map<String, Object> pageMap = couponService.pageCouponActivity(page, size);

        return JsonData.buildSuccess(pageMap);
    }
    @ApiOperation("领取优惠券")
    @GetMapping("add/promotion/{coupon_id}")
    public JsonData addPromotionCoupon(@ApiParam(value = "优惠券id", required = true) @PathVariable("coupon_id") long couponId) {
        JsonData jsonData = couponService.addCoupon(couponId, CouponCategoryEnum.PROMOTION);
        return jsonData;
    }
    //新人注册推送优惠券
    @ApiOperation("RPC-新用户注册接口")
    @PostMapping("/new_user_coupon")
    public JsonData addNewUserCoupon(@ApiParam("用户对象") @RequestBody NewUserCouponRequest newUserCouponRequest){
       JsonData jsonData= couponService.initNewCoupon(newUserCouponRequest);
       return jsonData;
    }

}

